package com.iwunu.iot.common.wechat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
@Component
public class OpenIdUtil {
	public static Logger log = LoggerFactory.getLogger(OpenIdUtil.class);
	public static JSONObject oauth2GetOpenid(String code) {
        String appid="wxe27d92d190138f08";
        String appsecret="4cb083b3e88d029804796d447b84cbf0";
        //授权（必填）
        String grant_type = "authorization_code";
        //URL
        String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";
        //请求参数
        String params = "appid=" + appid + "&secret=" + appsecret + "&js_code=" + code + "&grant_type=" + grant_type;
        //发送请求
        String data = HttpUtil.get(requestUrl, params);
        //解析相应内容（转换成json对象）
        JSONObject  json = JSONObject.parseObject(data);
        log.error("jscode2session:json "+ json);
        //用户的唯一标识（openid）
        return json;
    }
	
}
